package com.flexcloudsdk.oauth;

import java.util.logging.Logger;

import java.util.logging.Level;

import com.flexcloudsdk.users.CloudUser;
import com.flexcloudsdk.web.CloudAPIIdentityServlet;
import com.google.appengine.api.oauth.OAuthService;
import com.google.appengine.api.oauth.OAuthServiceFactory;
import com.google.appengine.api.oauth.OAuthRequestException;
import com.google.appengine.api.users.User;

@SuppressWarnings("unused")
public class CloudOauthService {
	private static final Logger log = Logger.getLogger(CloudOauthService.class.getName());
	private String sourceMethod;
	private String sourceClass;

	private OAuthService service = null;
	private User user;
	
	public CloudOauthService() {
		log.info("CloudOauthService has been created by BlazeDS");
		service = OAuthServiceFactory.getOAuthService();
		sourceClass = CloudOauthService.class.getName();
	}
	
	public User getCurrentUser() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		try {
			user = service.getCurrentUser();
		} catch (OAuthRequestException exception) {
			log.severe(exception.getMessage());
		}
		return user;
	}
	
	public boolean isUserAdmin() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		boolean flag = false;
		try {
		 flag = service.isUserAdmin();
		} catch (OAuthRequestException exception) {
			log.severe(exception.getMessage());
		}
		return flag;
	}
	
	public String getOAuthConsumerKey() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();

		String key = null;
		try {
			 key = service.getOAuthConsumerKey();
		} catch (OAuthRequestException exception) {
			log.severe(exception.getMessage());
		}
		return key;
	}
	
}
